package com.qh.servlet;

import com.qh.dao.Jdbc;
import com.qh.service.DataSyscise;
import com.qh.service.DataSysise;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * com.qh.servlet
 * 26832
 * yjj
 * &#064;description: 处理选择
 * &#064;author: lxm
 * IndexServlet
 * &#064;create: 2024-06-06 21:07
 **/
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession();
        String idcard = (String) session.getAttribute("idcard");
        System.out.println("idcard:"+idcard);
        List<String> allOptionsList = new ArrayList<>();

        // 安全地获取并添加每个参数数组到列表中，避免空指针异常
        addOptionsToList(req.getParameterValues("color[]"), allOptionsList, "颜色变化");
        addOptionsToList(req.getParameterValues("shape[]"), allOptionsList, "形态变化");
        addOptionsToList(req.getParameterValues("loose[]"), allOptionsList, "牙齿松动");
        addOptionsToList(req.getParameterValues("pain[]"), allOptionsList, "牙齿疼痛");
        addOptionsToList(req.getParameterValues("odor[]"), allOptionsList, "牙齿异味");
        addOptionsToList(req.getParameterValues("other[]"), allOptionsList, "其他症状");

        // 将列表转换为数组
        String[] allOptions = allOptionsList.toArray(new String[0]);
        //获取当前时间
        String time = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());

        //把症状存入数据表
        String sql ="insert into Describe1( idcard, `Describe`, data) VALUES('"+idcard+"','"+ Arrays.toString(allOptions) +"','"+time+"')";
        Connection con = null;
        Statement smt = null;
        Jdbc jdbc  = new Jdbc();
        try {
            con = jdbc.getConnection();
            smt = con.createStatement();
            int i = smt.executeUpdate(sql);
            if (i>0){
                System.out.println("插入成功");
            }else{
                System.out.println("插入失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        // 打印所有收集的选项
        for (String option : allOptions) {
            System.out.println(option);
        }
        //传递数组到data 处理业务，进行分析
        DataSyscise dataSyscise = new DataSysise();
        Object[] data =dataSyscise.data(allOptions);
        //遍历输出data
        for (Object o : data) {
            System.out.println(o);
        }
        //把结果转换为字符串，插入到数据表
        String sql1 ="insert into resoult(idcard, resoult1, resoult1data, resoult2, resoult2data, resoult3, resoult3data) VALUES('"+idcard+"','"+data[0]+"','"+data[1]+"','"+data[2]+"','"+data[3]+"','"+data[4]+"','"+data[5]+"')";
        Connection con1 = null;
        Statement smt1 = null;
        try {
            con1 = jdbc.getConnection();
            if (con != null) {
                smt1 = con.createStatement();
            }
            int j = 0;
            if (smt != null) {
                j = smt.executeUpdate(sql1);
            }
            if (j>0){
                System.out.println("结果插入成功");
                //转发页面到选择医生界面
                req.getRequestDispatcher("doctor.html").forward(req,resp);
            }else{
                System.out.println("结果插入失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                jdbc.close(con1,smt1,null);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

    }
    private void addOptionsToList(String[] options, List<String> list, String optionType) {
        if (options != null) {
            for (String option : options) {
                System.out.println(optionType + ": " + option);
                list.add(option);
            }
        } else {
            System.out.println(optionType + ": 无选择");
        }
    }
}
